package com.itboye.mengwu.app.ui.mymeng;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.core.alert.ByAlert;
import com.itboye.core.object.BaseActivity;
import com.itboye.core.object.ResultEntity;
import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.SPUtils;
import com.itboye.mengwu.R;
import com.itboye.mengwu.app.ui.aiwujia.YuErJingMomDetailActivity;
import com.itboye.mengwu.app.ui.aizhiwu.ActivityQueryWodeDongTaiDetail;
import com.itboye.mengwu.data.model.MePingLunbean;
import com.itboye.mengwu.data.presenter.MePingLunpter;
import com.itboye.mengwu.luntan.comment.adapter.ZamMeAdapter;
import com.itboye.mengwu.widget.XListView;
import com.itboye.mengwu.widget.XListView.IXListViewListener;

//import com.itboye.core.imageview.RoundImage;

public class ZanMeActivity extends BaseActivity implements Observer,
		IXListViewListener {

	private ZamMeAdapter adapter;

	/** Item数据实体集合 */
	/** ListView对象 */
	private XListView mePingLun;
	private ImageView onPinZanback;
	MePingLunpter presenter;
	String uid;
	ArrayList<MePingLunbean.MyPingLun> arrlist = new ArrayList<MePingLunbean.MyPingLun>();
	MePingLunbean lunbean;
	private TextView txtMsg;
	private int pageNo = 1;
	RelativeLayout nodata;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_pinglunme;
	}

	@Override
	public void onMyCreate() {
		txtMsg.setText("赞");
		mePingLun.setXListViewListener(this);
		mePingLun.setPullLoadEnable(true);
		mePingLun.setPullRefreshEnable(true);
		showProgressDialog("数据加载中，请稍后", true);
		uid = SPUtils.get(ZanMeActivity.this, null, SpPublicParams.UID, "")
				+ "";
		adapter = new ZamMeAdapter(this, arrlist);
		mePingLun.setAdapter(adapter);
		presenter = new MePingLunpter(this);

		lunbean = new MePingLunbean();
		presenter.dianzanMe(uid, pageNo + "");
		mePingLun.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				if (arrlist.get(position - 1).getFid().equals("4")) {
					Intent intent = new Intent(ZanMeActivity.this,
							WoDeDongTaiDetail2.class);
					intent.putExtra("tid", arrlist.get(position - 1).getTid());
					startActivity(intent);
				} else if (arrlist.get(position - 1).getFid().equals("6")
						|| arrlist.get(position - 1).getFid().equals("7")
						|| arrlist.get(position - 1).getFid().equals("8")) {// 育儿经
					Intent intent2 = new Intent(ZanMeActivity.this,
							YuErJingMomDetailActivity.class);
					intent2.putExtra("tid", arrlist.get(position - 1).getTid());
					startActivity(intent2);

				}
			}
		});
	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == MePingLunpter.DianZanMeSuccessEvent) {
				if (pageNo == 1) {
					arrlist.clear();
				}
				arrlist.addAll(((MePingLunbean) entity.getData()).getList());
				adapter.notifyDataSetChanged();
				if (arrlist.size() <= 0) {
					nodata.setVisibility(View.VISIBLE);
					mePingLun.setVisibility(View.GONE);
				} else {
					nodata.setVisibility(View.GONE);
					mePingLun.setVisibility(View.VISIBLE);
				}
			} else if (entity.getEventType() == MePingLunpter.DianZanMeFailEvent) {
				ByAlert.alert(entity.getData() + "");
			}
		}
		try {
			closeProgressDialog();
		} catch (Exception e) {
			// TODO: handle exception

		}
		stopLoad();

	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		pageNo = 1;
		presenter.dianzanMe(uid, pageNo + "");
	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		pageNo++;
		presenter.dianzanMe(uid, pageNo + "");
	}

	public void stopLoad() {
		mePingLun.stopLoadMore();
		mePingLun.stopRefresh();
	}

	public void onPinZanback(View view) {
		this.finish();
	}
}
